:- consult('../mem.pl').

iprint( [], _ ).
iprint( [Tile|Rest], Pos ) :-
	ifNewLine( Pos ),
	write( Tile ), 
	ifBar( Pos ),
	Next is Pos+1,
	iprint( Rest, Next ).
	
ifNewLine( 4 ) :- jump.
ifNewLine( 7 ) :- jump.
ifNewLine( _ ).

ifBar( 1 ) :- bar.
ifBar( 2 ) :- bar.
ifBar( 4 ) :- bar.
ifBar( 5 ) :- bar.
ifBar( 7 ) :- bar.
ifBar( 8 ) :- bar.
ifBar( _ ).

jump :- nl, margin, write('-----'), nl, margin.
bar :- write('|').
margin :- write('   ').

tateti( Tiles ) :- nl, margin, iprint( Tiles, 1 ), nl.

set_move( Pos, T ) :- mem( Pos, T ), play. 
player( 1, P ) :- set_move( P, x ).
player( 2, P ) :- set_move( P, o ).

turn( 1, 2 ).
turn( 2, 1 ).

choose( Pos ) :-
	mem( lastTurn, Last ),
	turn( Last, Turn ),
	mem( lastTurn, Turn ),
	player( Turn, Pos ).

play :-
	mem( [1,2,3,4,5,6,7,8,9], Tiles ),
	tateti( Tiles ).

start :- 
	S = ' ',
	mem([ 1/S,2/S,3/S,4/S,5/S,6/S,7/S,8/S,9/S, lastTurn/2 ]),
	play.